package org.gavin.config;

import org.gavin.service.impl.SubMsgListener;
import org.gavin.service.impl.SubMsgListener2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * RedisContainerListener
 *
 * @author grl
 * @date 2025/2/11
 */
@Configuration
public class RedisContainerListener {

    @Autowired
    SubMsgListener subMsgListener;


    /**
     * 消息侦听器容器
     *
     * @param factory 连接工厂
     * @return RedisMessageListenerContainer
     */
    @Bean
    RedisMessageListenerContainer redisContainer(final RedisConnectionFactory factory) {


        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(messageListener(), new ChannelTopic("redis-channel"));
        container.addMessageListener(messageListener2(), new ChannelTopic("redis-channel2"));
        return container;
    }

    /**
     * 消息监听适配器
     * MessageListenerAdapter
     *
     * @return
     */
    MessageListenerAdapter messageListener() {
        return new MessageListenerAdapter(subMsgListener);
    }

    /**
     * 消息监听适配器
     * MessageListenerAdapter
     *
     * @return
     */
    MessageListenerAdapter messageListener2() {
        return new MessageListenerAdapter(new SubMsgListener2());
    }
}
